package analysis.lexicalAnalysis;

import domain.StatusInfo;
import utils.CommonUtils;

import java.util.*;

/**
 * 词法分析相关类
 */
public class LexMain {

    private static final Scanner scan;

    // 状态转化变量
    private String[] chList;
    // 从左到右，第一个key代表行(转换前的状态)，
    // 第二个key代表列(状态转换的变量)，
    // 第二个value代表表项(转换后的状态)
    private Map<String, Map<String, ArrayList<String>>> statusMap;

    // NFA确定化后的状态转换表
    private StatusInfo newStatusInfo;

    // DFA最小化后的状态转换表
    private StatusInfo newStatusInfo2;

    // 开始状态
    private List<String> startStatus;

    //结束状态
    private List<String> endStatus;

    static {
        scan = new Scanner(System.in);
    }

    public LexMain() { }

    public LexMain(String[] chList, Map<String, Map<String, ArrayList<String>>> statusMap, List<String> startStatus, List<String> endStatus) {
        this.chList = chList;
        this.statusMap = statusMap;
        this.startStatus = startStatus;
        this.endStatus = endStatus;
    }

    public void lexMain(){
        System.out.println("+-----------------------------------+");
        System.out.println("|-------------词法分析---------------|");
        System.out.println("+-----------------------------------+");
        System.out.println("|-----------1、NFA确定化-------------|");
        System.out.println("+-----------------------------------+");
        System.out.println("|-----------2、DFA最小化-------------|");
        System.out.println("+-----------------------------------+");
        System.out.println("|-----------0、返    回--------------|");
        System.out.println("+-----------------------------------+");

        int itemChoose = -1;
        while (itemChoose != 0){
            System.out.print("\\-> ");
            try{
                itemChoose = Integer.parseInt(scan.next());
                switch (itemChoose){
                    case 0:{
                        System.out.println("|LexMain:lexMain(): 返回中...");
                        return;
                    }
                    case 1:{
                        System.out.println("+-----------------------------------+");
                        System.out.println("|NFA确定化前-------------------------|");
                        CommonUtils.printMap("|LexMain:lexMain():statusMap", statusMap);
                        System.out.println("+-----------------------------------+");
                        NfaToDfa nfaToDfa = new NfaToDfa(chList, statusMap, startStatus, endStatus);
                        newStatusInfo = nfaToDfa.nfaToDfa();
                        System.out.println("+-----------------------------------+");
                        String filePath = "source/test/lexicalTest/StatusChangeSecond.txt";
                        CommonUtils.printNewToFile(chList, newStatusInfo.getStatusChangeMap(), newStatusInfo.getStartStatus(), newStatusInfo.getEndStatus(), filePath);
                        System.out.println("+-----------------------------------+");
                        System.out.println("|NFA确定化后-------------------------|");
                        CommonUtils.printMap("|LexMain:lexMain():newStatusMap", newStatusInfo.getStatusChangeMap());
                        System.out.println("+-----------------------------------+");
                        break;
                    }
                    case 2:{
                        System.out.println("|LexMain:lexMain(): DFA最小化...");
                        DfaToMini dfaToMini = new DfaToMini(chList, newStatusInfo);
                        newStatusInfo2 = dfaToMini.dfaToMini();
                        String filePath = "source/test/lexicalTest/StatusChangeThird.txt";
                        CommonUtils.printNewToFile(chList, newStatusInfo2.getStatusChangeMap(), newStatusInfo2.getStartStatus(), newStatusInfo2.getEndStatus(), filePath);
                        System.out.println("+-----------------------------------+");
                        System.out.println("|NFA确定化后-------------------------|");
                        CommonUtils.printMap("|LexMain:lexMain():newStatusMap", newStatusInfo2.getStatusChangeMap());
                        System.out.println("+-----------------------------------+");
                        break;
                    }
                    default:{
                        System.out.println("|LexMain:lexMain(): 输入不匹配，请重新输入");
                    }
                }
            } catch (NumberFormatException numberFormatException){
                System.out.println("|LexMain:lexMain(): 输入不匹配，请重新输入");
            } catch (NoSuchElementException noSuchElementException){
                System.out.println("|LexMain:lexMain(): 输入为空");
            }
        }
        scan.close();
    }
}
